package api

import (
	"fmt"
	"io/ioutil"
	"runtime/debug"
	"strconv"

	"github.com/deploy-services/v2/KolonseWeb/HttpLib"
	"github.com/deploy-services/v2/KolonseWeb/Type"
	"github.com/deploy-services/v2/services"
)

func node_show(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	rest := services.NodesShow()
	res.Write([]byte(rest))
}

func node_cmd(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	node := req.URL.Query().Get("node")
	buf, _ := ioutil.ReadAll(req.Body)
	defer req.Body.Close()
	rest := services.NodeCMD(node, string(buf))
	res.Write([]byte(rest))
}

func node_enable(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	node := req.URL.Query().Get("node")
	enable_str := req.URL.Query().Get("enable")
	enable, _ := strconv.ParseBool(enable_str)
	rest := services.NodeEnable(node, enable)
	res.Write([]byte(rest))
}
